package com.vsoft.sms_cm;
import java.net.UnknownHostException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import com.vsoft.anti_fra.TestQuery;
import com.vsoft.framework.core.BusiObj;
import com.vsoft.framework.core.Configuration;
import com.vsoft.framework.server.servlet.SessionData;

import redis.clients.jedis.Jedis;
import com.vsoft.framework.core.BaseException;
import com.vsoft.framework.core.BusiObj;
import com.vsoft.framework.core.Configuration;
import com.vsoft.framework.core.db.DBGadgets;
import com.vsoft.framework.core.db.DBTrans;
import com.vsoft.framework.core.db.OperAgent;
import com.vsoft.framework.core.db.SQLArgs;
import com.vsoft.framework.core.esb.ServiceBus;
import com.vsoft.framework.core.util.BOHelper;
import com.vsoft.framework.core.util.LogCollect;
import com.vsoft.framework.core.util.XMLUtil;
import com.vsoft.framework.server.servlet.SessionData;
import com.vsoft.framework.server.servlet.HttpUtil;
public class testRedis {
	private static Logger vLogger = Logger.getLogger(testRedis.class);
	private static String vName = testRedis.class.getName();
	private SessionData vSD = null;
	private String vDebugHome = "";
	private BusiObj				vInBO		=  new BusiObj();
	private final static String		RESULT			="RESULT";
	public static void main(String[] args) throws BaseException  
	{
	
		String 	ip				=	args[0];
		String	port	=	args[1];
		//PropertyConfigurator.configure("log4j.properties");
		HashMap<String,String> dbConfig	= new HashMap<String,String>();

		try {
			Configuration.InitSystem();
			Logger vLogger = Logger.getLogger(testRedis.class);
			dbConfig.put("IP", ip);
			dbConfig.put("PORT", port);
			Jedis jedis = new Jedis(ip);
			Connection conn = DBTrans.getConnection();
/*			jedis.set("foo", "bar");
			String value = jedis.get("foo");*/
			jedis.flushDB();
			vLogger.info("---------------test redis dbSize-------------"+(jedis.dbSize()));
			//dbSize是多少个key的个数
			vLogger.info("=========1、查询数据库=======");
			long start_time_db=System.currentTimeMillis();
			String 	condition = " ";//and rownum<5 
			String sql 					=	TableCacheServerRedis.genSqlTriggerPhone(condition);
			SQLArgs 	Arg			= 	new SQLArgs();
			Arg.reset();
			vLogger.info("initialize cache (1)query Temple \n sql:" +sql);
			BusiObj		smsTempleBo 			= OperAgent.execQuery(conn, "getSubscriberAndTemple", sql, Arg);
			//处理短信模板，key=被叫号码，即触发电话
			long end_time_db=System.currentTimeMillis();
			ArrayList<BusiObj> alSms				=	smsTempleBo.getBusiObjList(RESULT);
			String order_num	= "";
			vLogger.info("=========1、分步更新redis缓存=======");
			String tableName	=	"trigger";
			String separator=":";
			long for_cnt_put=0;
			for(BusiObj m:alSms)//记录数7254，但实际是7360
			{
				order_num = m.getString("order_num");
				//vLogger.info("=========bo======="+m.getRawData());
				HashMap<String, Object> map =m.getRawData();
				HashMap<String, String> hash = new HashMap<String, String>(); 
				//hash=(HashMap<String, String>)m.getRawData();
				  
				  for   (String key  : map.keySet()){    
					  //System.out.println("键:" + key + "\t值:" + map.get(key));  
					  hash.put(key,(String)map.get(key));
					  
					  for_cnt_put++;
					  break;
			          //map.get(o);    
			  }  
				  jedis.hmset(tableName+separator+order_num, hash);
				  
				  jedis.lpush(tableName+separator+"order_num", order_num);//key's index
/*				jedis.lpush(order_num, m);
				jedis.lpush(key, string)*/
/*				ArrayList<BusiObj> AlBo			=	vData.get(order_num);
				if(AlBo==null)
					AlBo										=	new	ArrayList<BusiObj>();
				//m	=	TableCacheServerRedis.formatAlarmContext(m);
				AlBo.add(m);
				vData.put(order_num, AlBo);*/
			}
			long end_time_put=System.currentTimeMillis();
			long count = jedis.llen(tableName+separator+"order_num");
			ArrayList<String> al_list =(ArrayList)jedis.lrange(tableName+separator+"order_num", 0, count-1);
			long for_cnt_get=0;
			for(String listValue:al_list)
			{
				//vLogger.info("\n====================order_num==============\n"+listValue)	;
				//vLogger.info("\n====================get single hash====================\n")	;
				Map<String, String> map=jedis.hgetAll(tableName+separator+listValue);//"trigger:05514008801670"
				  for   (String key  : map.keySet()){    
					  //System.out.println("键:" + key + "\t值:" + map.get(key));  
					  for_cnt_get++;
			          //map.get(o);    
			  }  
			}
			long end_time_get=System.currentTimeMillis();

			jedis.disconnect();
			vLogger.info("\n====================init put vData over====================\n");
			vLogger.info("---------------test bo Size-------------"+(alSms.size()));
			vLogger.info("---------------test redis dbSize-------------"+(jedis.dbSize()));
			vLogger.info("---------------查询数据库耗时-------------"+(end_time_db-start_time_db));
			vLogger.info("---------------put redis耗时-------------"+(end_time_put-end_time_db));
			vLogger.info("---------------get redis耗时-------------"+(end_time_get-end_time_put));
			vLogger.info("\n=============for_cnt_get="+for_cnt_get+"================for_cnt_put="+for_cnt_put+"===========\n");
			
			long end_time=System.currentTimeMillis();
/*			vLogger.info("---------------start-------------"+start_time);
 * select 95680/7254 from dual
select 95680/7360 from dual
			long	count		=	10;//1w
			String collection = "testInsert";
			mdb.insertSample(db, collection, count);
			
			long batch_time=System.currentTimeMillis();
			vLogger.info("---------------insert 1w-------------"+(batch_time-start_time));
			count				=	1000000;//100w
			mdb.insertSample(db, collection, count);
			
			batch_time=System.currentTimeMillis();
			vLogger.info("---------------insert 100w-------------"+(batch_time-start_time));*/
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void  init()
	{
		
	}

}
